package com.example.pageradapterdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.pageradapterdemo.fragment.Demo1Fragment;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ViewPagerDemo1Activity extends AppCompatActivity {

    private ViewPager mContentVP;
    private List<String> dataList;
    private List<Fragment> fragmentList;
    private MyPagerAdapter mAdapter;
    private MyFragmentAdapter fragmentAdapter;
    private View view;
    private int position;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager_demo1);
        mContentVP = findViewById(R.id.content_vp);
        initData();
        mAdapter = new MyPagerAdapter();
        mContentVP.setAdapter(mAdapter);
        mContentVP.setCurrentItem(0);

        final Button btnUpdate = findViewById(R.id.btnUpdate);
        btnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataList.set(0,"更新数据源测试");
                mAdapter.notifyDataSetChanged();
            }
        });
        final Button btnAdd = findViewById(R.id.btnAdd);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataList.add("这是新添加的Item");
                mAdapter.notifyDataSetChanged();
            }
        });

        final Button btnRemove = findViewById(R.id.btnRemove);
        btnRemove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataList.remove(0);
                mAdapter.notifyDataSetChanged();
            }
        });

        final Button btnClear = findViewById(R.id.btnClear);
        btnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataList.clear();
                mAdapter.notifyDataSetChanged();
            }
        });

        mContentVP.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                ViewPagerDemo1Activity.this.position = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    private void initData(){
        dataList = new ArrayList<>();
        dataList.add("Java");
        dataList.add("Android");
        dataList.add("C&C++");
        dataList.add("OC");
        dataList.add("Swift");
        fragmentList = new ArrayList<>();
        for (String s : dataList) {
            fragmentList.add(Demo1Fragment.newInstance(s));
        }
    }

    public class MyPagerAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return dataList.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            view = getLayoutInflater().inflate(R.layout.adapter_viewpager_item,container,false);
            TextView tvTitle = view.findViewById(R.id.tv_title);
            tvTitle.setText("我的标题是：" + dataList.get(position));
            view.setTag(position);
            container.addView(view);
            Log.e("ss","title:" + dataList.get(position));
            return view;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);

        }

        @Override
        public int getItemPosition(@NonNull Object object) {
            View view = (View) object;
            int currentPosition = ViewPagerDemo1Activity.this.getCurrentPosition();
            if (currentPosition == (int)view.getTag()) {
                return POSITION_NONE;
            }else{
                return POSITION_UNCHANGED;
            }
        }
    }

    public class MyFragmentAdapter extends FragmentStatePagerAdapter{

        public MyFragmentAdapter(@NonNull FragmentManager fm) {
            super(fm, FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            super.destroyItem(container, position, object);
        }

    }

    public class MyFragmentPageAdapter extends FragmentPagerAdapter{

        public MyFragmentPageAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }

    private int getCurrentPosition(){
        return position;
    }
}